import pygame, sys, os
from pygame.locals import *
from maze import *
from builder import *
from player import *
import random

BOX_SIZE = 17

def input(events, player, enemy, maze):
    for event in events:
        if event.type == QUIT:
            sys.exit(0)
        elif event.type == KEYDOWN:
            scancode = event.dict['scancode']
            scanmap = {111: NORTH,
                       113: WEST,
                       114: EAST,
                       116: SOUTH}
            godir = scanmap.get(scancode, None)
            if godir is not None:
                if maze.can_go(player.x, player.y, godir):
                    nx, ny = godir.from_cell(player.x, player.y)
                    if enemy.alive and enemy.x == nx and enemy.y == ny:
                        patk = random.randint(0, 10)
                        eatk = random.randint(0, 20)
                        enemy.attack(patk)
                        player.attack(eatk)
                    else:
                        player.x, player.y = nx, ny
                    
        else:
	        print event 

def usage():
    print "python game.py WIDTH HEIGHT"
    sys.exit(-1)

def draw_maze(screen, maze):
    def mazeline(start, end, wall):
        color = (255, 255, 255)
        pygame.draw.line(screen, color, start, end)
    width = maze.width
    height = maze.height
    for x in range(width):
        for y in range(height):
            tx = BOX_SIZE + x * BOX_SIZE
            ty = BOX_SIZE + y * BOX_SIZE
            cell = maze.cells[x][y]
            if not cell.can_go(NORTH):
                mazeline((tx, ty), (tx+BOX_SIZE, ty), cell.walls[NORTH])
            if not cell.can_go(WEST):
                mazeline((tx, ty), (tx, ty+BOX_SIZE), cell.walls[WEST])
            if not cell.can_go(EAST):
                mazeline((tx+BOX_SIZE, ty), (tx+BOX_SIZE, ty+BOX_SIZE), cell.walls[EAST])
            if not cell.can_go(SOUTH):
                mazeline((tx, ty+BOX_SIZE), (tx+BOX_SIZE, ty+BOX_SIZE), cell.walls[SOUTH])

def draw_powerbar(screen, player, pos, size, color):
    pygame.draw.rect(screen, (255, 255, 255), Rect(pos, size), 1)
    if player.health > 0:
        barsize = ((size[0] - 4) * player.health // player.maxhealth, size[1] - 4)
        barpos = (pos[0] + 2, pos[1] + 2)
        pygame.draw.rect(screen, color, Rect(barpos, barsize))

def main(args):
    if len(args) < 2 or len(args) > 3:
        usage()
    try:
        width = int(args[0])
        height = int(args[1])
        if len(args) > 2:
            seed = int(args[2])
        else:
            seed = None
    except ValueError:
        usage()
    pygame.init()
    window_width = BOX_SIZE * 2 + width * BOX_SIZE
    window_height = BOX_SIZE * 3 + height * BOX_SIZE
    window = pygame.display.set_mode((window_width, window_height))
    pygame.display.set_caption('MAZE')
    screen = pygame.display.get_surface()
    maze = Maze(width, height)
    builder = MazeBuilder(maze, seed)
    player = Player(50, pygame.image.load(os.path.join("../data", "dude.png")))
    enemy = Player(25, pygame.image.load(os.path.join("../data", "dargon.png")))
    enemy.x = width // 4
    enemy.y = height // 4
    builder.build_maze()

    while True:
        screen.fill((0, 0, 0))
        draw_maze(screen, maze)
        draw_powerbar(screen, player, 
                      (BOX_SIZE, window_height - BOX_SIZE * 3 // 2),
                      (window_width // 2 - BOX_SIZE, BOX_SIZE),
                      (255, 0, 0))
        draw_powerbar(screen, enemy, 
                      (window_width // 2, window_height - BOX_SIZE * 3 // 2),
                      (window_width // 2 - BOX_SIZE, BOX_SIZE),
                      (0, 0, 255))
        if player.alive:
            screen.blit(player.image, (BOX_SIZE + player.x * BOX_SIZE,
                                       BOX_SIZE + player.y * BOX_SIZE))
        if enemy.alive:
            screen.blit(enemy.image, (BOX_SIZE + enemy.x * BOX_SIZE,
                                      BOX_SIZE + enemy.y * BOX_SIZE))
        pygame.display.flip()
        input(pygame.event.get(), player, enemy, maze)

if __name__ == '__main__':
    main(sys.argv[1:])
